﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Constructor
{
    public class NewItem1 : Object
    {
        private string _text;
        private Frame _head;
        public string Text
        {
            get
            {
                return ToString();
            }
            set
            {
                _text = value;
            }
        }
        public Slot Content { get; set; }
        public Frame Head
        {
            get {return _head;}
            set
            {
                _head= value;
                SetPath();
            }
        }
        public List<Slot> Path { get; set; }
        public NewItem1(Slot slot, string text)
        {
            Content = slot;
            Text = text;           
        }
        public override string ToString()
        {
            if (Content == null) return _text;
            if (Head == null) SetPath();
            if (Head != null)
            {
                string result = Head.Name + ".";
                foreach (Slot s in Path)
                    result += s.Name + ".";
                _text = result.TrimEnd('.');                
            }
            return _text;
        }
        public void SetPath()
        {
            string value = _text;
            int cnt = value.Split('.').Length;
            Frame f;
            if (cnt > 0 && Head == null)
                _head = External.SearchFrame(value.Split('.')[0]);
            f = Head;
            Path = new List<Slot>();
            Slot s = null;
            if (f != null)
                for (int i = 1; i < cnt; i++)
                {
                    s = f.SearchInAllSlot(value.Split('.')[i]);
                    if (s == null) return;
                    Path.Add(s);
                    f = s.Subframe;
                }
        }
        public NewItem1 Clone(Frame head)
        {
            return new NewItem1(Content, Text) { Head = head};            
        }
    }
}
